package com.dongdong.bookstore.module.pms.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongdong.bookstore.common.api.CommonPage;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.pms.dto.BookCategoryDto;
import com.dongdong.bookstore.module.pms.dto.BookCategroyNode;
import com.dongdong.bookstore.module.pms.model.BookCategory;
import com.dongdong.bookstore.module.pms.service.BookCategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 书籍分类表 前端控制器
 * </p>
 * /pms
 *
 * @author chiangkai
 * @since 2021-10-31
 */
@Api(tags = "书籍分类")
@RestController
public class BookCategoryController {
    @Autowired
    private BookCategoryService bookCategoryService;

    @PostMapping("/pms/bookCategory/create")
    public String create(@RequestBody @Validated BookCategoryDto dto) {
        Asserts.isFalse(bookCategoryService.create(dto), "添加失败");
        return "添加成功";
    }

    @GetMapping("/pms/bookCategory/delete/{id}")
    public String delete(@PathVariable Long id) {
        Asserts.isFalse(bookCategoryService.removeById(id), "删除失败");
        return "删除成功";
    }

    @PostMapping("/pms/bookCategory/update/{id}")
    public String update(@PathVariable Long id, @RequestBody @Validated BookCategoryDto dto) {
        Asserts.isFalse(bookCategoryService.update(id,dto), "更新失败");
        return "更新成功";
    }

    @GetMapping("/p/pms/bookCategory/list")
    public CommonPage<BookCategory> list(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "parentId", defaultValue = "0") Integer parentId,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        QueryWrapper<BookCategory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BookCategory::getParentId, parentId);
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.lambda().like(BookCategory::getName, keyword);
        }
        wrapper.lambda().orderByDesc(BookCategory::getSort);
        Page<BookCategory> page = bookCategoryService.page(new Page<>(pageNum, pageSize), wrapper);
        return CommonPage.restPage(page);
    }

    @GetMapping("/p/pms/bookCategory/listAll")
    public List<BookCategory> listAll() {
        QueryWrapper<BookCategory> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(BookCategory::getSort);
        return bookCategoryService.list(wrapper);
    }

    @GetMapping("/p/pms/bookCategory/listTree")
    public List<BookCategroyNode> listTree() {
        return bookCategoryService.listTree();
    }
}
